<div class="nav-sub w1180">
	<ul class="zoom">
		<li><a class="on" href="/crm/customer/list">客户列表</a></li>
		<li><a href="/crm/customer/feedback">留言管理</a></li>
	</ul>
</div>
<div class="main-ct w1180">
	<div class="top-bar">
		<div class="custom-search">
			<label>编号ID<input type="text" class="tp-txt" id="user_uid" style="width:100px;"></label>
			<label>客户昵称<input type="text" class="tp-txt" id="user_name" style="width:100px;"></label>
			<label>所属单位<select class="category-select" id="org_id" style="width:180px;">
				<option value="">未分类</option>
				<?php foreach ($this->organization as $organization):?>
				<option value="<?php echo $organization['org_id'];?>"><?php echo $organization['org_name'];?></option>
				<?php endforeach;?>
			</select></label>
			<a href="javascript:search();" class="btn-cs btn-search ml5">查 询</a>
			<a href="javascript:addBalance('ALL');" class="btn-cs btn-green ml5">客户批量充值</a>
			<a href="javascript:cleanBalance();" class="btn-cs btn-blue ml5">余额批量清零</a>
		</div>
	</div>
<div id="list-table"></div>
</div>
	<!-- Modal -->
	<div class="modal" id="addMoney" tabindex="-1" role="dialog" >
		<div class="modal-body">
			<button class="close" type="button" data-dismiss="modal">×</button>
			<form class="form-horizontal mt20" role="form">
			<input type="hidden" id="user_id" >
				<div class="form-group">
					<label class="col-sm-2 control-label">充值金额</label>
					<div class="col-sm-10" style="width: 380px;">
						<input type="number" class="form-control" id="addBalance" placeholder="元">
					</div>
				</div>
			</form>
			<button id="add_money" type="button" class="btn btn-primary fr">确定</button>
		</div>
	</div>
<script type="text/javascript">
    var user_name = $("#user_name").val();
    var org_id = $("#org_id").val();
    var user_uid = $("#user_uid").val();
    var page = 1;
    
    $(document).ready(function(){
    	ajaxListTable();
    });
    
    function search(){
    	user_name = $("#user_name").val();
    	org_id = $("#org_id").val();
    	user_uid = $("#user_uid").val();
    	page = 1;
    	ajaxListTable();
    }
   
    function ajaxListTable(){
    	$.ajax({
            type: "post",
            url: "<?php echo $this->url('crm/customer',
                array('action'=>'ajax-customer-list'));?>",
            dataType:'html',
            data: { "user_name": user_name, 
    				"page":page,
    				"user_uid":user_uid,
    				"org_id":org_id
    		},
            cache: false,
            timeout:10000, 
            error: function(){
              	 alert('请求超时，请重新操作');
               	 isPost = false;
                 return false; 
             }, 
            success: function(html){
            	$("#list-table").html(html);
            }
        });
    }
    function addBalance(user_id){
    	$('#user_id').val(user_id);
     	$('#addMoney').modal('show');
    }
    $("#add_money").click(function(){
        var user_id = $('#user_id').val();
        var add_balance = $('#addBalance').val();
        if(addBalance == ""){
        	alert('请填写充值金额！');
        	return;
        }
        if(user_id != 'ALL'){
        	$.ajax({
                type: "post",
                url : "<?php echo $this->url('crm/customer',
                    array('action'=>'add-balance'));?>",
                dataType:'json',
                data:  { 
                    "user_id": user_id, 
                    "add_balance": add_balance,
                     },
                cache: false,
                timeout:10000, 
                error: function(err){
                	alert('请求超时，请重新操作');
                    return false; 
               }, 
              success: function(result){
                  alert(result.reason);
                  $('#addMoney').modal('hide');
                  ajaxListTable();
              }
            });
        }else if(user_id == 'ALL'){
        	batchAddBalance();
        }
    });
    //批量充值
    function batchAddBalance(){
    	add_balance = $('#addBalance').val();
        if(!confirm("确认对查询结果批量充值？请谨慎使用！")){
            return;
        }
    	$.ajax({
            type: "post",
            url : "<?php echo $this->url('crm/customer',
                array('action'=>'batch-add-balance'));?>",
            dataType:'json',
            data:  { 
            	"add_balance": add_balance,
            	"user_name": user_name, 
				"user_uid":user_uid,
				"org_id":org_id
    			},
            cache: false,
            timeout:10000, 
            error: function(err){
              	 alert('请求超时，请重新操作');
                 return false; 
             }, 
            success: function(result){
                alert(result.reason);
                $('#addMoney').modal('hide');
                ajaxListTable();
            }
        });
    }
    //批量清零
    function cleanBalance(){
        if(!confirm("确认对查询结果批量清零？请谨慎使用！")){
            return;
        }
    	$.ajax({
            type: "post",
            url : "<?php echo $this->url('crm/customer',
                array('action'=>'batch-clean-balance'));?>",
            dataType:'json',
            data:  { 
            	"user_name": user_name, 
				"user_uid":user_uid,
				"org_id":org_id
    			},
            cache: false,
            timeout:10000, 
            error: function(err){
              	 alert('请求超时，请重新操作');
                 return false; 
             }, 
            success: function(result){
                alert(result.reason);
                ajaxListTable();
            }
        });
    }
</script>